﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using Microsoft.Win32;

using RemoteScriptingLibrary.Microsoft.Win32;

namespace RemoteScriptingLibrary
{
    public class RemoteControl
    {
        public static void SynthesizeKeystrokesToWindow(string sPartialWindowCaptionName, string sText)
        {
            SynthesizeKeystrokesToWindow(sPartialWindowCaptionName, sText, "Term Class", true);
        }

        public static void Click(MouseButtons buttons)
        {
            Win32.POINT point = new Win32.POINT();
            Win32.GetCursorPos(out point);
            Click(buttons, (uint)point.X, (uint)point.Y, MOUSESTATE.CLICK);
        }

        public enum MOUSESTATE
        {
            CLICK,
            DOWN,
            UP
        }

        public static void Click(MouseButtons buttons, uint x, uint y, MOUSESTATE _state)
        {
            Cursor.Position = new System.Drawing.Point((int)x, (int)y);
            switch (buttons)
            {
                case MouseButtons.Left:

                    if (_state == MOUSESTATE.CLICK || _state == MOUSESTATE.DOWN)
                    {
                        Win32.mouse_event((int)(Win32.MouseEventFlags.LEFTDOWN), x, y, 0, 0);
                    }
                    if (_state == MOUSESTATE.CLICK || _state == MOUSESTATE.UP)
                    {
                        Win32.mouse_event((int)(Win32.MouseEventFlags.LEFTUP), x, y, 0, 0);
                    }
                    break;

                case MouseButtons.Right:
                    if (_state == MOUSESTATE.CLICK || _state == MOUSESTATE.DOWN)
                    {
                        Win32.mouse_event((int)(Win32.MouseEventFlags.RIGHTDOWN), x, y, 0, 0);
                    }
                    if (_state == MOUSESTATE.CLICK || _state == MOUSESTATE.UP)
                    {
                        Win32.mouse_event((int)(Win32.MouseEventFlags.RIGHTUP), x, y, 0, 0);
                    }
                    break;

                case MouseButtons.Middle:
                    if (_state == MOUSESTATE.CLICK || _state == MOUSESTATE.DOWN)
                    {
                        Win32.mouse_event((int)(Win32.MouseEventFlags.MIDDLEDOWN), x, y, 0, 0);
                    }
                    if (_state == MOUSESTATE.CLICK || _state == MOUSESTATE.UP)
                    {
                        Win32.mouse_event((int)(Win32.MouseEventFlags.MIDDLEUP), x, y, 0, 0);
                    }
                    break;
            }
        }

        public static void ClickMenu()
        {
        }

        public static void ClickMenu(string _sWindowName, string _sClassName, string _sMenuItemName, MouseButtons _mb)
        {
        }

        /*public static void ClickMenu(IntPtr ptrWinHandle, string _sMenuItemName, MouseButtons _mb)
        {
        } */

        public static void ClickMenu(string _sMenuItemName, MouseButtons _mb)
        {
        }

        public static void ClickMenu(IntPtr ptrWinHandle, string _sMenuItemName, MouseButtons _mb)
        {
        }

        public static void MoveCursorOverControl(IntPtr _ptrHandle)
        {
            IntPtr handle = IntPtr.Zero;
            Point point;
            int x = 0;
            int y = 0;
            int width = 0;
            int height = 0;
            bool coordinatesFound = false;

            if (_ptrHandle != null)
            {
                Win32.RECT rect = new Win32.RECT();
                Win32.GetWindowRect(_ptrHandle, out rect);
                width = rect.Width;
                height = rect.Height;
                handle = _ptrHandle;
            }

            if ((width > 0) && (height > 0))
            {
                point = new Point();
                x = (width / 2);
                y = (height / 2);

                coordinatesFound = Win32.ClientToScreen(handle, ref point);

                if (coordinatesFound == true)
                {
                    Win32.SetCursorPos(point.X + x, point.Y + y);
                }
            }
        }

        public static void Click(IntPtr ptrControl, MouseButtons _buttons)
        {
            Helper.Click(ptrControl, _buttons);
        }

        public static void SynthesizeKeystrokesToWindow(string sPartialWindowCaptionName, string sText, bool _bIncludeReturn)
        {
            SynthesizeKeystrokesToWindow(sPartialWindowCaptionName, null, sText, _bIncludeReturn);
        }

        public static void SynthesizeKeystrokesToWindow(string sPartialWindowCaptionName, string sClassName, Keys key)
        {
            SetSynthesizeKeyPressWindow(sPartialWindowCaptionName, sClassName);
            Win32.PressKey(key);
        }


        public static void SynthesizeKeystrokesToWindow(string sPartialWindowCaptionName, string sClassName, params Keys[] aKey)
        {
            SetSynthesizeKeyPressWindow(sPartialWindowCaptionName, sClassName);
            Win32.PressKeyArray(aKey);
        }

        public static void SynthesizeKeystrokesToWindow(string sPartialWindowCaptionName, string sClassName, string sText, bool _bIncludeReturn)
        {
            //this.Text = Helper.GetWindowTextRaw(button1.Handle);
            //listBox1.Items.AddRange(DACHelperWrapper.GetOpenedFiles().ToArray());
            SetSynthesizeKeyPressWindow(sPartialWindowCaptionName, sClassName);

            TextToKeyPressArray(sText, _bIncludeReturn);

        }

        internal static void TextToKeyPressArray(string sText, bool _bIncludeReturn)
        {
            Keys[] aKey = new Keys[sText.Length + 1];
            int nIndex = 0;
            foreach (char chChar in sText)
            {
                aKey[nIndex++] = (Keys)(byte)char.ToUpper(chChar);
            }
            if (_bIncludeReturn)
            {
                aKey[nIndex] = Keys.Enter;
            }

            Win32.PressKeyArray(aKey);
        }

        private static void SetSynthesizeKeyPressWindow(string sPartialWindowCaptionName, string sClassName)
        {
            IntPtr ptrHyperTerminal = Helper.GetWindowFromPartialCaption(sPartialWindowCaptionName);
            List<IntPtr> aEdit = Helper.GetChildWindows(ptrHyperTerminal);

            List<IntPtr> aWindow = Helper.GetWindowByClassName(aEdit, sClassName);
            if (aWindow.Count > 0)
            {
                Win32.SetActiveWindow(aWindow[0]);
                Win32.SetForegroundWindow(aWindow[0]);
                Win32.SetFocus(aWindow[0]);
            }
        }
    }
}
